package com.purong.purong.activity;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.purong.purong.R;
import com.purong.purong.util.SharePreUtil;
import com.purong.purong.widget.LoadingDialog;
import com.yao.sdk.net.NetWorkListener;
import com.yao.sdk.net.ResponseInfo;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public abstract class BaseActivity extends NetActivity {
    private Map<String, BroadcastReceiver> receiverMap;
    private LoadingDialog mDialog;
    private String loadingUrl;
    private long dialogShowTime;
    private final long MIN_SHOW_TIME = 500;
    private boolean isNeverAskAgain = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(getRootViewId());
        initBundle();
        initView();
        initData();

    }
    public abstract int getRootViewId();
    public abstract void initView();
    public abstract void initData();

    public void initBundle(){};
    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    public void fullscreen(boolean enable) {
        if (enable) { //显示状态栏
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            getWindow().setAttributes(lp);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        } else { //隐藏状态栏
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            getWindow().setAttributes(lp);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }
    }


    public void showLogo() {
        findViewById(R.id.center_logo).setVisibility(View.VISIBLE);
    }

    public void showBack() {
        ImageView iv = (ImageView) findViewById(R.id.left_back);
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                BaseActivity.this.finish();
            }
        });
        iv.setVisibility(View.VISIBLE);
        findViewById(R.id.left_image).setVisibility(View.GONE);
    }

    public void showBack(OnClickListener listener) {
        ImageView iv = (ImageView) findViewById(R.id.left_back);
        iv.setOnClickListener(listener);
        iv.setVisibility(View.VISIBLE);
        findViewById(R.id.left_image).setVisibility(View.GONE);
    }

    public void showLeft(int resId, OnClickListener listener) {
        ImageView iv = ((ImageView) findViewById(R.id.left_image));
        iv.setImageResource(resId);
        iv.setOnClickListener(listener);
        iv.setVisibility(View.VISIBLE);
        findViewById(R.id.left_back).setVisibility(View.GONE);
    }

    public void showLeft(int resId) {
        ImageView iv = ((ImageView) findViewById(R.id.left_image));
        iv.setImageResource(resId);
        iv.setVisibility(View.VISIBLE);
        findViewById(R.id.left_back).setVisibility(View.GONE);

    }

    public void showRightTv(String s, OnClickListener listener) {
        TextView tv = (TextView) findViewById(R.id.right_tv);
        tv.setText(s);
        tv.setOnClickListener(listener);
        tv.setVisibility(View.VISIBLE);
    }

    public void showRight(int resId, OnClickListener listener) {
        ImageView iv = ((ImageView) findViewById(R.id.right_image));
        iv.setImageResource(resId);
        iv.setOnClickListener(listener);
        iv.setVisibility(View.VISIBLE);
    }

    public void showRight(OnClickListener listener) {
        ImageView iv = ((ImageView) findViewById(R.id.right_image));
        iv.setOnClickListener(listener);
        iv.setVisibility(View.VISIBLE);
    }

    public void showRightSecond(int resId) {
        ImageView iv = ((ImageView) findViewById(R.id.right_image2));
        iv.setImageResource(resId);
        iv.setVisibility(View.VISIBLE);
    }

    public void showRightSecond(int resId, OnClickListener listener) {
        ImageView iv = ((ImageView) findViewById(R.id.right_image2));
        iv.setImageResource(resId);
        iv.setOnClickListener(listener);
        iv.setVisibility(View.VISIBLE);
    }

    public void showRightthred(int resId, OnClickListener listener) {
        ImageView iv = ((ImageView) findViewById(R.id.right_image3));
        iv.setImageResource(resId);
        iv.setOnClickListener(listener);
        iv.setVisibility(View.VISIBLE);
    }

    public void showRightSecond(OnClickListener listener) {
        ImageView iv = ((ImageView) findViewById(R.id.right_image2));
        iv.setOnClickListener(listener);
        iv.setVisibility(View.VISIBLE);
    }

    public void showRight(int resId) {
        ImageView iv = ((ImageView) findViewById(R.id.right_image));
        iv.setImageResource(resId);
        iv.setVisibility(View.VISIBLE);
    }

    public void showTitle(String title) {

        TextView tv = (TextView) findViewById(R.id.center_title);
//		TextPaint tp = tv.getPaint();
//		tp.setFakeBoldText(true);
        tv.setText(title);
    }

    public void showTitleBold(String title) {

        TextView tv = (TextView) findViewById(R.id.center_title);
        TextPaint tp = tv.getPaint();
        tp.setFakeBoldText(true);
        tv.setText(title);
    }


    @Override
    protected void onDestroy() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
        destroyReceiver();
        super.onDestroy();
        if (mDialog != null) {
            mDialog = null;
        }
    }

    public void showToast(String toast) {
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
    }

    public void showToast(String toast, int length) {
        Toast.makeText(this, toast, length).show();
    }


    public boolean request(String url, Map<String, String> params, boolean addToken, NetWorkListener listener, boolean showDialog) {
        boolean isRequest = super.request(url, params, addToken ? SharePreUtil.getInstance(this).getToken() : null, listener);
        if (showDialog && isRequest) {
            if (mDialog == null) {
                mDialog = new LoadingDialog(this);
            }
            if (!hasDestroy())
                mDialog.show();
            dialogShowTime = System.currentTimeMillis();
            loadingUrl = url;
        }
        return isRequest;
    }

    public boolean request(String url, Map<String, String> params, boolean addToken, boolean showDialog) {
        boolean isRequest = super.request(url, params, addToken ? SharePreUtil.getInstance(this).getToken() : null);
        if (showDialog && isRequest) {
            if (mDialog == null) {
                mDialog = new LoadingDialog(this);
            }
            if (!hasDestroy())
                mDialog.show();
            dialogShowTime = System.currentTimeMillis();
            loadingUrl = url;
        }
        return isRequest;
    }

    public boolean request(String url, Map<String, String> params, boolean addToken) {
        return super.request(url, params, addToken ? SharePreUtil.getInstance(this).getToken() : null);
    }
    public boolean requestNoToast(String url, Map<String, String> params, boolean addToken, boolean showDialog) {
        boolean isRequest = super.requestNoToast(url, params, addToken ? SharePreUtil.getInstance(this).getToken() : null);
        if (showDialog && isRequest) {
            if (mDialog == null) {
                mDialog = new LoadingDialog(this);
            }
            if (!hasDestroy())
                mDialog.show();
            dialogShowTime = System.currentTimeMillis();
            loadingUrl = url;
        }
        return isRequest;
    }

    public boolean requestNoToast(String url, Map<String, String> params, boolean addToken) {
        return super.requestNoToast(url, params, addToken ? SharePreUtil.getInstance(this).getToken() : null);
    }
    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (mDialog != null && mDialog.isShowing() && responseInfo.url.equals(loadingUrl)) {
            long time = System.currentTimeMillis() - dialogShowTime;
            if (time < MIN_SHOW_TIME) {
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if (mDialog != null) {
                            mDialog.dismiss();
                        }
                    }
                }, MIN_SHOW_TIME - time);
            } else {
                if (mDialog != null) {
                    mDialog.dismiss();
                }
            }

        }
        super.onResponse(responseInfo);
    }

    public void showDialog() {

        if (mDialog == null) {
            mDialog = new LoadingDialog(this);
        }
        if (!hasDestroy())
            mDialog.show();
    }

    public void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    public void sendBroadcast(String action, String s) {
        Intent intent = new Intent();
        intent.setAction(action);
        intent.putExtra("String", s);
        sendBroadcast(intent);
    }

    public void sendBroadcast(String action, int i) {
        Intent intent = new Intent();
        intent.setAction(action);
        intent.putExtra("int", i);
        sendBroadcast(intent);
    }

    public void sendBroadcast(String action, boolean i) {
        Intent intent = new Intent();
        intent.setAction(action);
        intent.putExtra("boolean", i);
        sendBroadcast(intent);
    }

    public void sendBroadcast(String action, Serializable object) {
        Intent intent = new Intent();
        intent.setAction(action);
        intent.putExtra("object", object);
        sendBroadcast(intent);
    }

    public void sendBroadcast(String action) {
        Intent intent = new Intent();
        intent.setAction(action);
        sendBroadcast(intent);
    }

    public void addAction(String action, BroadcastReceiver receiver) {
        try {
            IntentFilter filter = new IntentFilter();
            filter.addAction(action);
            registerReceiver(receiver, filter);
            if (receiverMap == null) {
                receiverMap = new HashMap<>();
            }
            receiverMap.put(action, receiver);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 销毁广播
     */
    public void destroyReceiver() {
        if (receiverMap != null) {
            BroadcastReceiver receiver;
            for (String key : receiverMap.keySet()) {
                receiver = receiverMap.get(key);
                if (receiver != null) {
                    unregisterReceiver(receiver);
                }
            }
        }
    }

    //	@Override
//	public void onPermissionsGranted(int i, List<String> list) {
//
//	}
//
//	@Override
//	public void onPermissionsDenied(int i, List<String> list) {
////		isNeverAskAgain = EasyPermissions.checkDeniedPermissionsNeverAskAgain(this, getString(R.string.rationale_ask_again),
////				"设置", android.R.string.cancel, list);
//	}
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public boolean hasDestroy() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
            return false;
        if (isDestroyed()) {
            return true;
        } else {
            return false;
        }
    }

}
